package com.example.music.service.Impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.dao.AdminMapper;
import com.example.music.dto.AdminDto;
import com.example.music.entity.Admin;
import com.example.music.service.AdminService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Security;
import java.util.HashMap;
import java.util.List;


@Service
public class AdminServiceImpl extends BaseServiceImpl<Admin,AdminMapper> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    protected AdminMapper getMapper() {
        return adminMapper;
    }

    @Override
    public List<Admin> verifyPassword(AdminDto adminDto) {
        String username = adminDto.getUsername();
        String password = adminDto.getPassword();
        String md5 = SecureUtil.md5(password);
        System.out.println(md5);
        HashMap<String, Object> params = Maps.newHashMap();
        params.put("name",username);
        params.put("password",md5);
        return adminMapper.selectByMap(params);
    }

    @Override
    public void md5Password() {
        List<Admin> users = adminMapper.selectList(null);
        for (Admin user : users) {
            UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("password", SecureUtil.md5(user.getPassword()));
            adminMapper.update(user,updateWrapper);
        }
    }
}
